package edu.sdjzu.exam.mapper.examSession;

import edu.sdjzu.exam.domain.examSession.Answer;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AnswerMapper {

    @Insert("INSERT INTO exam_Answers (exam_id, student_id, question_id, answer_content, file_path, is_draft, answer_time) " +
            "VALUES (#{examId}, #{studentId}, #{questionId}, #{answerContent}, #{filePath}, #{isDraft}, #{answerTime})")
    @Options(useGeneratedKeys = true, keyProperty = "answerId")
    void insertAnswer(Answer answer);

    @Update("UPDATE exam_Answers SET answer_content = #{answerContent}, file_path = #{filePath}, is_draft = #{isDraft}, " +
            "answer_time = #{answerTime}, updated_at = CURRENT_TIMESTAMP WHERE answer_id = #{answerId}")
    void updateAnswer(Answer answer);

    @Select("SELECT * FROM exam_Answers WHERE exam_id = #{examId} AND student_id = #{studentId} AND question_id = #{questionId} AND is_draft = true")
    Answer selectDraftByExamAndStudentAndQuestion(@Param("examId") Long examId, @Param("studentId") Long studentId, @Param("questionId") Long questionId);

    @Select("SELECT * FROM exam_Answers WHERE exam_id = #{examId} AND student_id = #{studentId} AND is_draft = true")
    List<Answer> selectDraftsByExamAndStudent(@Param("examId") Long examId, @Param("studentId") Long studentId);

    @Select("SELECT * FROM exam_Answers WHERE exam_id = #{examId} AND is_draft = false")
    List<Answer> selectAnswersByExamId(Long examId);

    @Update("UPDATE exam_Answers SET is_draft = false, answer_time = #{answerTime}, updated_at = CURRENT_TIMESTAMP " +
            "WHERE exam_id = #{examId} AND student_id = #{studentId} AND is_draft = true")
    void submitDrafts(@Param("examId") Long examId, @Param("studentId") Long studentId, @Param("answerTime") java.time.LocalDateTime answerTime);

    @Select("SELECT * FROM exam_Answers WHERE session_id = #{sessionId} AND question_id = #{questionId}")
    Answer selectAnswerBySessionAndQuestion(@Param("sessionId") Long sessionId, @Param("questionId") Long questionId);
}